﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Reddit.EndpointRetriever
{
    class UrlEndpointRetriever : IEndpointRetriever<string>
    {
        protected string host { get; set; }
        protected string extension { get; set; }
        public UrlEndpointRetriever(string host, string extension) 
        {
            this.host = host;
            this.extension = extension;
        }
        #region IEndpointRetriever<string> Members

        public string RetrieveEndpoint(Endpoint endpoint)
        {
            return "http://" + host
                + "/" 
                + String.Join("/", 
                    endpoint.Pieces
                    .SelectMany(p => p.Split('/'))
                    .Select(p => Uri.EscapeDataString(p))
                    .ToArray())
                + "." + extension + "?"
                + String.Join("&", (from e in endpoint.Options
                                    select Uri.EscapeDataString(e.Key)
                                        + "="
                                        + Uri.EscapeDataString(e.Value)).ToArray());
            /*return "http://" + host 
                + "/" + String.Join("/", endpoint.Pieces.ToArray()) 
                + "." + extension + "?" 
                + String.Join("&", endpoint.Options.Select(o => o.ToString()).ToArray());*/
        }

        #endregion
    }
}
